package com.wbao.dianniu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wbao.dianniu.R;
import com.wbao.dianniu.data.ChatNotifyData;
import com.wbao.dianniu.utils.DateUtils;
import com.wbao.dianniu.utils.PictureDownloadUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yejianjun on 2018/6/2.
 */

public class ChatNotifyAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater inflater;
    private List<ChatNotifyData> list = new ArrayList<>();

    public ChatNotifyAdapter(Context context) {
        mContext = context;
        inflater = LayoutInflater.from(context);
    }


    public void setData(List<ChatNotifyData> list) {
        this.list = list;
    }

    public void unReadChatChange(int idType) {
        for (int i = 0; i < list.size(); i++) {
            if (idType == list.get(i).getId()) {
                list.get(i).setUnReadCount(0);
                notifyDataSetChanged();
                break;
            }
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHodler holder = null;
        if (null == convertView) {
            holder = new ViewHodler();
            convertView = inflater.inflate(R.layout.notify_list_item, null);
            holder.headIV = (ImageView) convertView.findViewById(R.id.head);
            holder.unReadTV = (TextView) convertView.findViewById(R.id.unread);
            holder.nameTV = (TextView) convertView.findViewById(R.id.name);
            holder.atMsgTV = (TextView) convertView.findViewById(R.id.at_msg_notify);
            holder.contentTV = (TextView) convertView.findViewById(R.id.content);
            holder.timeTV = (TextView) convertView.findViewById(R.id.time);
            convertView.setTag(holder);
        } else {
            holder = (ViewHodler) convertView.getTag();
        }

        ChatNotifyData data = list.get(position);
        if (null != data) {
            PictureDownloadUtils.displayHeadNotPrefix(mContext, holder.headIV, data.getIconUrl());
            holder.nameTV.setText(data.getName() == null ? "" : data.getName());
            if (data.getUnReadCount() > 0) {
                holder.unReadTV.setText(data.getUnReadCount() + "");
                holder.unReadTV.setVisibility(View.VISIBLE);

            } else {
                holder.unReadTV.setVisibility(View.INVISIBLE);
            }
            holder.atMsgTV.setVisibility(View.GONE);
            holder.contentTV.setText(data.getLastContent() == null ? "" : data.getLastContent());
            holder.timeTV.setText(DateUtils.getNewChatTime(data.getLastUpdateDate()));
        }

        return convertView;
    }


    class ViewHodler {
        private ImageView headIV;
        private TextView unReadTV;
        private TextView nameTV;
        private TextView atMsgTV;
        private TextView contentTV;
        private TextView timeTV;
    }


}
